//
//  CLRMTopView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/27.
//

#import "CLRMTopView.h"
#import "CLReadController.h"
#import "CLReadModel+Mark.h"
@interface CLRMTopView()
/// 返回
@property (nonatomic, strong) UIButton *back;

/// 书签
@property (nonatomic, strong) UIButton *mark;
@end
@implementation CLRMTopView

- (void)addSubviews {
    [super addSubviews];
    
    // 返回
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.back = back;
    [back setImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    back.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:back];
    
    // 书签
    UIButton *mark = [UIButton buttonWithType:UIButtonTypeCustom];
    self.mark = mark;
    mark.contentMode = UIViewContentModeCenter;
    [mark setImage:[[UIImage imageNamed:@"mark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [mark addTarget:self action:@selector(clickMark:) forControlEvents:UIControlEventTouchUpInside];
    mark.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:mark];
    [self updateMarkButton];
}

- (void)clickBack {

    if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickBack:)]) {
        [self.readMenu.delegate readMenuClickBack:self.readMenu];
    }
}

- (void)clickMark:(UIButton *)button {
    
    if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickMark:topView:markButton:)]) {
        [self.readMenu.delegate readMenuClickMark:self.readMenu topView:self markButton:button];
    }
    
}

/// 检查是否存在书签
- (void)checkForMark {
    
    self.mark.selected = [self.readMenu.vc.readModel isExistMark:nil] != nil;
    
    [self updateMarkButton];
}

- (void)updateMarkButton {
    if (self.mark.isSelected) {
        self.mark.tintColor = CL_READ_COLOR_MAIN;
    } else {
        self.mark.tintColor = CL_READ_COLOR_MENU_COLOR;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat y = STATUSBAR_HEIGHT;
    
    CGFloat wh = NavgationBarHeight - y;
    
    self.back.frame = CGRectMake(0, y, wh, wh);
    
    self.mark.frame = CGRectMake(self.frame.size.width - wh, y, wh, wh);
}
@end
